<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
    :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item label="林权证号" prop="forestOwnershipCertificateNumber">
            <el-input v-model="form.forestOwnershipCertificateNumber" placeholder="请输入林权证号"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="注销类型" prop="cancellationType">
            <el-input v-model="form.cancellationType" placeholder="请输入注销类型"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="申请人" prop="applicant">
            <el-input v-model="form.applicant" placeholder="请输入申请人"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="审核人" prop="reviewer">
            <el-input v-model="form.reviewer" placeholder="请输入审核人"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="审核时间" prop="auditTime">
            <el-date-picker type="date" placeholder="请选择审核时间" v-model="form.auditTime" :value-format="dateStr"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="审核结果" prop="auditResult">
            <el-input v-model="form.auditResult" placeholder="请输入审核结果"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="申请人身份证号" prop="applicantsIdNumberNumber">
            <el-input v-model="form.applicantsIdNumberNumber" placeholder="请输入申请人身份证号"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="申请人联系电话" prop="applicantsContactPhoneNumber">
            <el-input v-model="form.applicantsContactPhoneNumber" placeholder="请输入申请人联系电话"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="是代理申请" prop="itsAProxyApplication">
            <el-input v-model="form.itsAProxyApplication" placeholder="请输入是代理申请"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="代理申请人姓名" prop="nameOfProxyApplicant">
            <el-input v-model="form.nameOfProxyApplicant" placeholder="请输入代理申请人姓名"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="代理申请人身份证号" prop="agentApplicantIdNumberNumber">
            <el-input v-model="form.agentApplicantIdNumberNumber" placeholder="请输入代理申请人身份证号"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="代理申请人联系电话" prop="contactNumberOfTheProxyApplicant">
            <el-input v-model="form.contactNumberOfTheProxyApplicant" placeholder="请输入代理申请人联系电话"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="申请附件" prop="applyForAttachments">
            <el-input v-model="form.applyForAttachments" placeholder="请输入申请附件"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="代理附件" prop="proxyAttachment">
            <el-input v-model="form.proxyAttachment" placeholder="请输入代理附件"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="注销附件" prop="cancelAttachment">
            <el-input v-model="form.cancelAttachment" placeholder="请输入注销附件"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="登记类型" prop="registrationType">
            <el-input v-model="form.registrationType" placeholder="请输入登记类型"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="受理日期" prop="acceptDate">
            <el-date-picker type="date" placeholder="请选择受理日期" v-model="form.acceptDate" :value-format="dateStr"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="业务编号" prop="businessNumber">
            <el-input v-model="form.businessNumber" placeholder="请输入业务编号"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="LogOutRegistrationDialog">
// ========== 1. 导入语句 ==========
import { useDict } from '/@/hooks/dict';
import { rule } from '/@/utils/validate';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj, validateExist } from '/@/api/forestry/logOutRegistration';

// ========== 2. 组件定义 ==========
// 定义组件事件
const emit = defineEmits(['refresh']);

// ========== 3. 响应式数据定义 ==========
// 基础响应式变量
const dataFormRef = ref(); // 表单引用
const visible = ref(false); // 弹窗显示状态
const loading = ref(false); // 加载状态

// 表单数据对象
const form = reactive({
  id: '', // 主键
  forestOwnershipCertificateNumber: '', // 林权证号
  cancellationType: '', // 注销类型
  applicant: '', // 申请人
  reviewer: '', // 审核人
  auditTime: '', // 审核时间
  auditResult: '', // 审核结果
  applicantsIdNumberNumber: '', // 申请人身份证号
  applicantsContactPhoneNumber: '', // 申请人联系电话
  itsAProxyApplication: '', // 是代理申请
  nameOfProxyApplicant: '', // 代理申请人姓名
  agentApplicantIdNumberNumber: '', // 代理申请人身份证号
  contactNumberOfTheProxyApplicant: '', // 代理申请人联系电话
  applyForAttachments: '', // 申请附件
  proxyAttachment: '', // 代理附件
  cancelAttachment: '', // 注销附件
  registrationType: '', // 登记类型
  acceptDate: '', // 受理日期
  businessNumber: '', // 业务编号
});

// ========== 4. 字典数据处理 ==========

// ========== 5. 表单校验规则 ==========
const dataRules = ref({
  forestOwnershipCertificateNumber: [
    { required: true, message: '林权证号不能为空', trigger: 'blur' }
  ],
  cancellationType: [
    { required: true, message: '注销类型不能为空', trigger: 'blur' }
  ],
  applicant: [
    { required: true, message: '申请人不能为空', trigger: 'blur' }
  ],
});

// ========== 6. 方法定义 ==========
// 获取详情数据
const getLogOutRegistrationData = async (id: string) => {
  try {
    loading.value = true;
    const { data } = await getObj({ id: id });
    // 直接将第一条数据赋值给表单
    Object.assign(form, data[0]);
  } catch (error) {
    useMessage().error('获取数据失败');
  } finally {
    loading.value = false;
  }
};

// 打开弹窗方法
const openDialog = (id: string) => {
  visible.value = true;
  form.id = '';

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取LogOutRegistration信息
  if (id) {
    form.id = id;
    getLogOutRegistrationData(id);
  }
};

// 提交表单方法
const onSubmit = async () => {
  loading.value = true; // 防止重复提交
  
  // 表单校验
  const valid = await dataFormRef.value.validate().catch(() => {});
  if (!valid) {
    loading.value = false;
    return false;
  }

  try {
    // 根据是否有ID判断是新增还是修改
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh'); // 通知父组件刷新列表
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// ========== 7. 对外暴露 ==========
// 暴露方法给父组件
defineExpose({
  openDialog
});
</script> 